//
//  JingOneViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/8.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "JingOneViewController.h"

#import "ViewController.h"
#import "ZiViewController.h"//个人资料
#import "MyLetterViewController.h"//我的站内信
#import "ManagerHuiVC.h"
//#import "MYongJinVC.h"
#import "MYeJiVC.h"

#import "PeopleOneCell.h"
#import "PeopleTwoCell.h"
#import "TodoHttpManager.h"
@interface JingOneViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    NSString *uid;
    NSArray *_tiArr;
    NSArray *_imArr;
   
}
@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableDictionary *managerDic;
@end

@implementation JingOneViewController

-(void)viewWillAppear:(BOOL)animated
{
    
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    uid= [userDefa objectForKey:@"USER_ID"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGetMineGetInfo:) name:TDGetMineGetInfo object:nil];
    [_manager userGetMineInfoGetWithID:uid];
    
    
}


-(void)didTDGetMineGetInfo:(NSNotification *)tific
{
    if (tific.object != nil)
    {
        _managerDic = [tific.object objectForKey:@"data"];
        // NSLog( @"里边的跟人信息--peopleDic-0--%@",_peopleDic);
        
        
        
        [_tabView reloadData];
        
        
        
        
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:TDGetMineGetInfo object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人中心";
    self.view.backgroundColor = BgColor;
    
    
//    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 74, B_WIDTH, 90)];
//    topView.backgroundColor =[UIColor whiteColor];
//    [self.view addSubview:topView];
//    _oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _oneBtn.frame = CGRectMake(20, 10, 70, 70);
//    _oneBtn.tag = 35;
//    _oneBtn.layer.cornerRadius = 35;
//    _oneBtn.clipsToBounds = YES;
//    
//    [_oneBtn addTarget:self action:@selector(oneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [topView addSubview:_oneBtn];
//
//    
//    [self setUpUIButton];
//    
//    UIButton *tuiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    tuiBtn.frame = CGRectMake(15, 420, B_WIDTH - 15*2, 40);
//    tuiBtn.backgroundColor = OverColor;
//    tuiBtn.layer.cornerRadius = 5;
//    [tuiBtn setTitle:@"退出" forState:0];
//    [tuiBtn setTitleColor:[UIColor whiteColor] forState:0];
//    [tuiBtn addTarget:self action:@selector(tuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:tuiBtn];
  
    
    
    
//    _tiArr = @[@"",@"     我的站内信",@"    优惠申请",@"    销售佣金",@"    案场业绩"];
//    _imArr = @[@"",@"znx",@"xiaoshouMoney",@"yongjin",@"yeji"];

//    _tiArr = @[@"",@"     我的站内信",@"    优惠申请",@"    案场业绩"];
//    _imArr = @[@"",@"znx",@"xiaoshouMoney",@"yeji"];
    
    _tiArr = @[@"",@"     我的站内信",@"    优惠申请"];
    _imArr = @[@"",@"znx",@"xiaoshouMoney"];
    _manager = [TodoHttpManager sharedManager];
    _managerDic = [NSMutableDictionary dictionary];
    
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    
    _tabView.backgroundColor = BgColor;
    [_tabView registerClass:[PeopleOneCell class] forCellReuseIdentifier:@"CellE"];
    [_tabView registerClass:[PeopleTwoCell class] forCellReuseIdentifier:@"CellA"];
    
    [self.view addSubview:_tabView];
    

    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tiArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        NSString *IDCell = @"Cell";
        PeopleOneCell *cell = [tableView dequeueReusableCellWithIdentifier:IDCell];
        if (cell == nil)
        {
            cell = [[PeopleOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDCell];
        }
        
        if ([cell isKindOfClass:[PeopleOneCell class]])
        {
            if (_managerDic != nil)
            {
                [(PeopleOneCell *)cell setupPeopleCellWithHeaderImage:[_managerDic objectForKey:@"img"] withNickName:[_managerDic objectForKey:@"name"]];
            }
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        return cell;
    }
    else
    {
        
        NSString *CellIdentifier1 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        PeopleTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell==nil)
        {
            
            cell = [[PeopleTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        }
        
        cell.tuImg.image = [UIImage imageNamed:_imArr[indexPath.row]];
        
        cell.titleLab.text = _tiArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 80;
    }
    else
    {
        return 50;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 100)];
    view.backgroundColor = BgColor;
    [self.view addSubview:view];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, 30, B_WIDTH-20*2, 40);
    btn.layer.cornerRadius = 5;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"退出" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(JingLiTuiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0:{
            ZiViewController *mainVC = [[ZiViewController alloc] init];
            [self.navigationController pushViewController:mainVC animated:NO];
        }
            
            break;
        case 1:{
            MyLetterViewController *xinVC = [[MyLetterViewController alloc] init];
            [self.navigationController pushViewController:xinVC animated:NO];
            
        }
            
            break;
        case 2:{
            ManagerHuiVC *huiVC = [[ManagerHuiVC alloc] init];
            [self.navigationController pushViewController:huiVC animated:NO];
            
        }
            
            break;
//        case 3:{
//            MYongJinVC *jinVc = [[MYongJinVC alloc] init];
//            [self.navigationController pushViewController:jinVc animated:NO];
//            
//        }
            
          //  break;
        case 3:{
//            MYeJiVC *jiVC = [[MYeJiVC alloc] init];
//            [self.navigationController pushViewController:jiVC animated:NO];
            
        }
            
            break;
        default:
            break;
    }

}

-(void)JingLiTuiBtnClick:(UIButton *)btn
{
    //切换到主线程执行
    [self performSelectorOnMainThread:@selector(showExitAlert) withObject:self waitUntilDone:NO];
    
    

    
    
    
    
}

-(void)showExitAlert
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"确定要退出?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UID"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USER_ID"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"NetHeadImage"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LocalHeadImage"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"STATE"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PROJECT_NAME"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LOGIN_NAME"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"ONLY_URL"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"BTG"];
        
        
        
        ViewController *viewCC = [[ViewController alloc] init];
        [self presentViewController:viewCC animated:NO completion:nil];
        
        
    }]];
    
    [alert.view setTintColor:[UIColor colorWithRed:37/255.0 green:85/255.0 blue:100/255.0 alpha:1]];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
